package com.dahai.gameinfo.controller;

import com.dahai.gameinfo.ResultData;
import com.dahai.gameinfo.model.ListClass;
import com.dahai.gameinfo.model.ListData;
import com.dahai.gameinfo.model.LoginData;
import com.dahai.gameinfo.model.UserSelect;
import com.dahai.gameinfo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@RestController()
public class VueController {

    @Autowired
    DemoService service;

    /**
     * 添加游戏分类
     */
    @GetMapping("/vue/addClass")
    public ResultData addClass(@RequestParam("name")String name,
                               @RequestParam("userid") int userid,
                               @RequestParam("token") String token) {
        ResultData result = new ResultData();
        ListClass clazz = new ListClass();
        boolean b = tokenId(userid, token);
        if (!b) {
            result.setState(-1);
            result.setMessage("token校验失败");
            return result;
        }
        clazz.setName(name);
        service.saveCLass(clazz);
        result.setObject(clazz);
        return result;
    }

    /**
     * 添加游戏
     */
    @PostMapping("/vue/addGame")
    public ResultData addGame(@RequestBody ListData clazz) {
        ResultData result = new ResultData();
        service.save(clazz);
        result.setObject(clazz);
        return result;
    }

    /**
     * 添加游戏
     */
    @GetMapping("/vue/addGameGet")
    public ResultData getAddGame(@RequestParam("title")String title,
                               @RequestParam("size")double size,
                               @RequestParam("classId")int classId,
                               @RequestParam("hot")int hot,
                               @RequestParam("icon")String icon,
                               @RequestParam("userid") int userid,
                               @RequestParam("token") String token
                               ) {
        ResultData result = new ResultData();
        ListData data = new ListData();
        boolean b = tokenId(userid, token);
        if (!b) {
            result.setMessage("token校验失败");
            result.setState(-1);
            return result;
        }
        data.setTitle(title);
        data.setSize(size);
        data.setClassId(classId);
        data.setHot(hot);
        data.setIcon(icon);
        service.save(data);
        result.setObject(data);
        return result;
    }

    /**
     * 获取游戏分类
     */
    @GetMapping("/vue/getClassList")
    public ResultData getClassList() {
        List<ListClass> list = service.getClassType();
        list.sort(Comparator.comparingInt(ListClass::getId));
        ResultData result = new ResultData();
        result.setList(list);
        return result;
    }

    /**
     * 根据分类id获取游戏列表
     */
    @GetMapping("/vue/getGameList")
    public ResultData getGameList(@RequestParam("clazz") int clazz, @RequestParam("page") int page) {
        ResultData result = new ResultData();
        page = page-1;
        if (page < 0) {
            page = 0;
        }
        List<ListData> list = service.getListByClass(clazz, page * 10);
        int listDataCount = service.getListDataCount(clazz);
        result.setCount(listDataCount);
        result.setList(list);
        return result;
    }

    /**
     * 删除
     */
    @GetMapping("/vue/deleteGame")
    public ResultData deleteGame(@RequestParam("id") int id, @RequestParam("userid") int userid, @RequestParam("token") String token) {
        ResultData result = new ResultData();
        boolean b = tokenId(userid, token);
        if (!b) {
            result.setState(-1);
            result.setMessage("token校验失败");
            return result;
        }
        service.deleteGame(id);
        return result;
    }

    @GetMapping("/vue/updateGameSize")
    public ResultData updateGameSize(@RequestParam("id") int id, @RequestParam("size") double size, @RequestParam("userid") int userid, @RequestParam("token") String token) {
        ResultData result = new ResultData();
        boolean b = tokenId(userid, token);
        if (!b) {
            result.setState(-1);
            result.setMessage("token校验失败");
            return result;
        }
        ListData listData = new ListData();
        listData.setId(id);
        listData.setSize(size);
        service.updateGameSize(listData);
        result.setObject(listData);
        return result;
    }

    /**
     * 登录
     */
    @GetMapping("/vue/login")
    public ResultData login(@RequestParam("username") String username, @RequestParam("password") String password) {
        ResultData result = new ResultData();
        LoginData data = new LoginData();
        data.setUsername(username);
        data.setPassword(password);
        List<LoginData> login = service.login(data);
        if (login.size() >0) {
            data.setToken(String.valueOf(System.currentTimeMillis()));
            data.setId(login.get(0).getId());
        }
        service.updateToken(data);
        data.setPassword("");
        result.setObject(data);
        return result;
    }

    @GetMapping("/vue/getGameListByGameId")
    public ResultData getGameListByGameId(@RequestParam("id") int id) {
        ResultData result = new ResultData();
        List<ListData> list = new ArrayList<>();
        if (id!=0) {
            List<UserSelect> userSelects = service.getSelectById(id);
            if (userSelects.size() > 0) {
                UserSelect select = userSelects.get(0);
                String gameId = select.getGameId();
                if (gameId != null) {
                    String[] split = gameId.split(",");
                    for (String s : split) {
                        if (s.isEmpty()) {
                            continue;
                        }
                        List<ListData> listById = service.getListById(Integer.parseInt(s));
                        list.addAll(listById);
                    }
                }
            }
        }
        result.setList(list);
        return result;
    }

    public boolean tokenId(int id, String token) {
        if (token == null) {
            return false;
        }
        List<LoginData> loginData = service.tokenId(id);
        if (loginData.size() > 0) {
            return token.equals(loginData.get(0).getToken());
        }
        return false;
    }
}
